﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.Web;

namespace simple.Utilities
{
    public class SendMail
    {
        public static bool SendEmail(string strToEmail, string strSubject, string strBody, AlternateView htmlView)
        {
            try
            {
                //Create Mail Message Object with content that you want to send with mail.
                System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(ConfigurationManager.AppSettings["sysEmail"] + " <" + ConfigurationManager.AppSettings["sysEmail"] + ">", strToEmail,
                strSubject, strBody);

                MyMailMessage.IsBodyHtml = true;
                MyMailMessage.AlternateViews.Add(htmlView);

                //Proper Authentication Details need to be passed when sending email from gmail
                System.Net.NetworkCredential mailAuthentication = new
                System.Net.NetworkCredential(ConfigurationManager.AppSettings["sysEmail"], ConfigurationManager.AppSettings["sysPassword"]);

                //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
                //For different server like yahoo this details changes and you can
                //get it from respective server.
                System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["sysMailServer"], Convert.ToInt32(ConfigurationManager.AppSettings["sysMailServerPort"]));

                //Enable SSL
                mailClient.EnableSsl = true;

                mailClient.UseDefaultCredentials = false;

                mailClient.Credentials = mailAuthentication;

                mailClient.Send(MyMailMessage);
                return true;
            }
            catch //(System.Web.HttpException ehttp) 
            {
                return false;
            }
        }

        public static AlternateView GetEmailViewFromTemplateTemplate(string toName,string CMND,string address,string phone, string email,
                                                                     string bookingCode, string hotel, string checkin, string checkout,
                                                                     string roomType, string roomCount,string price, string payType)
        {
            StringBuilder emailTemplate = new StringBuilder();
            string filePath = HttpContext.Current.Server.MapPath("EmailTemplate.html");

            AlternateView htmlView = null;
            if (System.IO.File.Exists(filePath))
            {
                emailTemplate = new StringBuilder(System.IO.File.ReadAllText(filePath));
                emailTemplate.Replace("<%Name%>", toName);
                emailTemplate.Replace("<%CMND%>", CMND);
                emailTemplate.Replace("<%Address%>", address);
                emailTemplate.Replace("<%Phone%>", phone);
                emailTemplate.Replace("<%Email%>", email);
                emailTemplate.Replace("<%Bookingcode%>", bookingCode);
                emailTemplate.Replace("<%Hotel%>", hotel);
                emailTemplate.Replace("<%Checkin%>", checkin);
                emailTemplate.Replace("<%Checkout%>", checkout);
                emailTemplate.Replace("<%Roomtype%>", roomType);
                emailTemplate.Replace("<%Roomcount%>", roomCount);
                emailTemplate.Replace("<%Price%>", price);
                emailTemplate.Replace("<%Paytype%>", payType);

                htmlView = AlternateView.CreateAlternateViewFromString(emailTemplate.ToString(), null, "text/html");
            }

            return htmlView;
        }
    }
}
